package com.tracesystem.service.impl;

import com.tracesystem.commons.Result;
import com.tracesystem.controller.form.ActivRoleForm;
import com.tracesystem.controller.form.BanRoleForm;
import com.tracesystem.controller.form.ChangeRoleInfoForm;
import com.tracesystem.controller.form.RoleForm;
import com.tracesystem.model.IssueRole;
import com.tracesystem.dao.IssueRoleDao;
import com.tracesystem.service.IssueRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tracesystem.util.WoniuCopyUtil;
import com.tracesystem.util.exception.IssueRoleException;
import com.tracesystem.util.exception.code.IssueRoleExceptionCode;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Kevin Martin
 * @since 2022-03-23
 */
@Service
public class IssueRoleServiceImpl extends ServiceImpl<IssueRoleDao, IssueRole> implements IssueRoleService {

    @Resource
    private IssueRoleDao roleDao;

    @Override
    public Result<?> addRole(RoleForm roleForm) {

        IssueRole issueRole = WoniuCopyUtil.copyOne(roleForm, IssueRole.class);
        issueRole.setModuleState("有效");
        roleDao.insertRole(issueRole);
        return Result.getInstance().setMsg("添加角色成功");


    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result<?> changeRoleinfo(ChangeRoleInfoForm changeRoleInfoForm) {
        IssueRole issueRole = getById(changeRoleInfoForm.getRoleId());
        if (null == issueRole) {
            throw new IssueRoleException(IssueRoleExceptionCode.ISSUE_ROLE_NOTFINDEXCEPTION_CODE);
        }
        issueRole.setModifyBy(changeRoleInfoForm.getModifyBy());
        issueRole.setRoleName(changeRoleInfoForm.getRoleName());
        updateById(issueRole);
        return Result.getInstance().setMsg("修改成功");

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result<?> stopRole(BanRoleForm banRoleForm) {
        if (banRoleForm.getRoleId() == 0) {
            throw new IssueRoleException(IssueRoleExceptionCode.ISSUE_ROLE_MANAGERCANTBAN_ERROR);
        }
        IssueRole issueRole = getById(banRoleForm.getRoleId());
        if (null == issueRole) {
            throw new IssueRoleException(IssueRoleExceptionCode.ISSUE_ROLE_NOTFINDEXCEPTION_CODE);
        }
        if ("禁止".equals(issueRole)) {
            throw new IssueRoleException(IssueRoleExceptionCode.ISSUE_ROLE_ARADYBAN_ERROR);
        }
        issueRole.setModuleState("禁止");
        updateById(issueRole);
        return Result.getInstance().setMsg("禁止角色成功");

    }

    @Override
    public Result<?> activeRole(ActivRoleForm activRoleForm) {
        IssueRole issueRole = getById(activRoleForm.getRoleId());
        if (null == issueRole) {
            throw new IssueRoleException(IssueRoleExceptionCode.ISSUE_ROLE_NOTFINDEXCEPTION_CODE);
        }
        if ("启用".equals(issueRole)) {
            throw new IssueRoleException(IssueRoleExceptionCode.ISSUE_ROLE_ARADYACTIVE_ERROR);
        }
        issueRole.setModuleState("启用");
        updateById(issueRole);
        return Result.getInstance().setMsg("启用角色成功");

    }
}
